/*
 * Filename:    EvaluationCodeUtil.java
 * Copyright:   2015 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2015-2-9
 */
package com.paic.mhis.hcpms.evaluation.common.biz.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.paic.mhis.hcpms.evaluation.common.biz.service.EvaluationCodeService;
import com.paic.mhis.hcpms.evaluation.common.dao.CodeRuleDao;
import com.paic.mhis.hcpms.evaluation.common.dto.CodeRuleDTO;

/**
 * 诚信考评编号
 * 
 * @author EX-PANAN001
 * 
 */
@Component("evaluationCodeService")
public class EvaluationCodeServiceImpl implements EvaluationCodeService {
	@Autowired
	private CodeRuleDao codeRuleDao;

	@Override
	public String getEvaluationCode(String type, String prefix, String date) {
		return prefix + getCodeStr(type, date);
	}

	/**
	 * 生成生成诚信考评编号
	 * 
	 * @param type
	 *            编号类型 （“3”诚信）
	 * @return
	 */

	private String getCodeStr(String type, String date) {
		StringBuffer strbuf = new StringBuffer();
		strbuf.append(date);
		CodeRuleDTO crt = codeRuleDao.getCodeNum(type);
		int num = Integer.parseInt(crt.getNum()) + 1;
		if (num < 10) {
			strbuf.append("000" + num);
		} else if (num < 100) {
			strbuf.append("00" + num);
		} else if (num < 1000) {
			strbuf.append("0" + num);
		} else if (num < 10000) {
			strbuf.append(num);
		} else {
			num = 1;
			strbuf.append("000" + num);
		}
		crt.setNum(num + "");
		codeRuleDao.updateCodeRule(crt);
		return strbuf.toString();
	}

}
